#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* openUBMC is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*         http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

IR global 语句节点定义
"""

import ast
from dataclasses import dataclass, field
from typing import List, Iterator, Optional
from .base import IRStmt
from ..base import IRNode
from ...symbol_table import Symbol, SymbolKind


@dataclass
class IRGlobal(IRStmt):
    """global 语句"""

    names: List[str] = field(default_factory=list)
    node_type: str = "global"

    def new_symbol(self) -> "Symbol":
        """创建 global 语句的符号"""
        # global 语句本身不创建符号，它只是声明全局变量
        raise NotImplementedError("global 语句不创建符号")

    def build_from_ast(self, node: ast.Global) -> None:
        """从 AST global 节点构建 IR global 节点内容

        Args:
            node: AST global 节点
        """
        super().build_from_ast(node)
        self.names = node.names.copy()

    def get_child_nodes(self) -> Iterator["IRNode"]:
        """获取所有子节点"""
        # global 语句没有子节点
        yield from super().get_child_nodes()


@dataclass
class IRNonlocal(IRStmt):
    """nonlocal 语句"""

    names: List[str] = field(default_factory=list)
    node_type: str = "nonlocal"

    def new_symbol(self) -> "Symbol":
        """创建 nonlocal 语句的符号"""
        # nonlocal 语句本身不创建符号，它只是声明非局部变量
        raise NotImplementedError("nonlocal 语句不创建符号")

    def build_from_ast(self, node: ast.Nonlocal) -> None:
        """从 AST nonlocal 节点构建 IR nonlocal 节点内容

        Args:
            node: AST nonlocal 节点
        """
        super().build_from_ast(node)
        self.names = node.names.copy()

    def get_child_nodes(self) -> Iterator["IRNode"]:
        """获取所有子节点"""
        # nonlocal 语句没有子节点
        yield from super().get_child_nodes()
